﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.Common;
using QunYing.Common;
using QunYing.Model;

namespace QunYing.Web.market
{
    public partial class ordertwo : System.Web.UI.Page
    {
        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Repeater rp = ((Repeater)Master.FindControl("NavRepeater"));
                rp.DataSource = NavData();
                rp.DataBind();
            }
        }
        /// <summary>
        /// 快捷导航数据
        /// </summary>
        private DataTable NavData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("name");
            dt.Columns.Add("url");
            dt.Rows.Add("购物车结算", "orderone.aspx");
            dt.Rows.Add("确认订单信息", "");
            return dt;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                BindAdd();
            }
        }
        private void BindData()
        {
            DataTable dt = (DataTable)Session["ShopCart"];
            GridView1.DataSource = dt;
            GridView1.DataBind();
            if (dt != null)
            {
                decimal totalPrice = 0;
                int useScore = 0;
                int getScore = 0;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    totalPrice += Convert.ToDecimal(dt.Rows[i]["单价"]) * Convert.ToInt32(dt.Rows[i]["数量"]);
                    useScore += Convert.ToInt32(dt.Rows[i]["消耗积分"]) * Convert.ToInt32(dt.Rows[i]["数量"]);
                    getScore += Convert.ToInt32(dt.Rows[i]["赠送积分"]) * Convert.ToInt32(dt.Rows[i]["数量"]);
                }
                lblTotalPrice.Text = totalPrice.ToString("f2");
                lblUseScore.Text = useScore.ToString();
                lblGetScore.Text = getScore.ToString();
            }

        }
        private void BindAdd()
        {
            Model.VipInfo model = new BLL.VipInfo().GetModelByID(((ArrayList)Session["userInfo"])[0].ToString());
            txtUser.Text = model.用户姓名;
            txtAddress.Text = model.通信地址;
            txtTel.Text = model.移动电话;
        }

        protected void btnGo_Click(object sender, ImageClickEventArgs e)
        {
            DataTable dt = (DataTable)Session["ShopCart"];
            if (dt != null)
            {
                if (dt.Rows.Count == 0)
                {
                    MessageBox.Show(this, "没有选购商品");
                    return;
                }
            }
            if (Session["userInfo"] == null)
            {
                Response.Redirect("~/Login.aspx");
            }
            Model.Score model = new BLL.Score().GetModel(((ArrayList)Session["userInfo"])[0].ToString());
            if (model == null)
            {
                model = new Score();
                model.用户ID = ((ArrayList)Session["userInfo"])[0].ToString();
                model.积分 = 0;
                new QunYing.BLL.Score().Add(model);
            }
            if (int.Parse(lblUseScore.Text) > model.积分)
            {
                MessageBox.Show(this, "没有足够积分兑换商品");
                return;
            }
            int res = new BLL.OrderInfo().AddOrder(((ArrayList)Session["userInfo"])[0].ToString(), ((ArrayList)Session["userInfo"])[1].ToString(), txtUser.Text.Trim(),
                                         txtAddress.Text.Trim(), txtTel.Text.Trim(), txtMsg.Text.Trim(), dt);
            if (res > 0)
            {
                try
                {
                    IOrderInfoReceived wr = ClientProxyFactory.GetProxy<IOrderInfoReceived>(GlobalInfo.ServiceOrderInfoReceived);
                    wr.OrderInfoReceived(res, ((ArrayList)Session["userInfo"])[4].ToString());
                    ClientProxyFactory.Close(wr);
                }
                catch (Exception)
                {
                    
                }


                Session.Remove("ShopCart");
                Response.Redirect("orderthree.aspx?id=" + res);
            }
            else
            {
                MessageBox.Show(this, "下单失败,请重试!");
            }
        }
    }
}
